package org.opens.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties(value = {MinioConfigProperties.class})
public class MinioConfig {

    @Autowired
    private MinioConfigProperties minioConfigProperties;

    @Bean
    public MinioClient getMinioClient() throws Exception {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioConfigProperties.getUrl())
                .credentials(minioConfigProperties.getAccessKey(), minioConfigProperties.getSecretKey())
                .build();

        //判断桶是否存在，不存在则新建
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConfigProperties.getBucketName()).build())){
            minioClient.makeBucket(MakeBucketArgs.builder()
                    .bucket(minioConfigProperties.getBucketName())
                    .build());
        }

        return minioClient;
    }

}
